import * as Auth from '../actions/auth.actions';
import {createFeatureSelector, createSelector} from '@ngrx/store';

export interface UserModel {
  userId?: string;
  userRole?: number;
  userType?: number;
  toBindCard?: boolean;
  toRestPWd?: boolean;
  mobile?: string;
  password?: string;
}

export interface ResponseModel {
  code?: number;
  msg?: string;
}

export interface AuthState {
  status?: number;
  userInfo?: UserModel;
  response?: ResponseModel;
}

export const initialState: AuthState = {
  status: 0,
  userInfo: {},
  response: null
};

export function reducer(state = initialState, action: Auth.ActionTypeUnion): AuthState {
  switch (action.type) {

    case Auth.ActionTypes.Login:
      return state;

    case Auth.ActionTypes.LoginSuccess:
      return {...state, ...action.payload};

    case Auth.ActionTypes.LoginFailure:
      return state;

    case Auth.ActionTypes.Logout:
      return {...state, ...action.payload};

    default:
      return state;

  }
}

export const getAuthState = createFeatureSelector<AuthState>('authState');

export const getAuth = createSelector(
  getAuthState,
  (state: AuthState) => state
);
